package com.sumi.erp.controller;

import com.sumi.common.annotation.Log;
import com.sumi.common.core.controller.BaseController;
import com.sumi.common.core.domain.AjaxResult;
import com.sumi.common.core.page.TableDataInfo;
import com.sumi.common.enums.BusinessType;
import com.sumi.common.utils.poi.ExcelUtil;
import com.sumi.common.utils.poi.ExcelUtils;
import com.sumi.erp.domain.BaseBusinessPartner;
import com.sumi.erp.service.IBaseBusinessPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 商业伙伴Controller
 * 
 * @author fgf67@163.com
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/erp/partner")
public class BaseBusinessPartnerController extends BaseController
{
    @Autowired
    private IBaseBusinessPartnerService baseBusinessPartnerService;

    /**
     * 查询商业伙伴列表
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseBusinessPartner baseBusinessPartner)
    {
        startPage();
        List<BaseBusinessPartner> list = baseBusinessPartnerService.selectBaseBusinessPartnerList(baseBusinessPartner);
        return getDataTable(list);
    }

    /**
     * 导出商业伙伴列表
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:export')")
    @Log(title = "商业伙伴", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseBusinessPartner baseBusinessPartner)
    {
        List<BaseBusinessPartner> list = baseBusinessPartnerService.selectBaseBusinessPartnerList(baseBusinessPartner);
        ExcelUtil<BaseBusinessPartner> util = new ExcelUtil<BaseBusinessPartner>(BaseBusinessPartner.class);
        util.exportExcel(response, list, "商业伙伴数据");
    }

    @PreAuthorize("@ss.hasPermi('erp:partner.import')")
    @Log(title = "商业伙伴", businessType = BusinessType.EXPORT)
    @PostMapping("/import")
    public AjaxResult importFile(MultipartFile file) throws Exception {
        try {
            if (file.isEmpty()) {
                return AjaxResult.warn("文件不能为空");
            }
            InputStream inputStream = file.getInputStream();
            List<List<String>> lists = ExcelUtils.getInstance().readExcel2List(inputStream, 1, 2, 0);
            System.out.println("读取Excel至String数组：");
            for (List<String> list : lists) {
                System.out.println(list);
            }
            return AjaxResult.success(lists);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 获取商业伙伴详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseBusinessPartnerService.selectBaseBusinessPartnerById(id));
    }

    /**
     * 新增商业伙伴
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:add')")
    @Log(title = "商业伙伴", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseBusinessPartner baseBusinessPartner)
    {
        return toAjax(baseBusinessPartnerService.insertBaseBusinessPartner(baseBusinessPartner));
    }

    /**
     * 修改商业伙伴
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:edit')")
    @Log(title = "商业伙伴", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseBusinessPartner baseBusinessPartner)
    {
        return toAjax(baseBusinessPartnerService.updateBaseBusinessPartner(baseBusinessPartner));
    }

    /**
     * 删除商业伙伴
     */
    @PreAuthorize("@ss.hasPermi('erp:partner:remove')")
    @Log(title = "商业伙伴", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseBusinessPartnerService.deleteBaseBusinessPartnerByIds(ids));
    }
}
